package com.principles.behavior.mediator;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description 中介者模式：能让你减少对象之间混乱无序的依赖关系。该模式会限制对象之间的直接交互，迫使他们通过一个中介者对象进行合作。
 * @Date 2025/1/31 10:20
 **/
public class MediatorPattern {
    public static void main(String[] args) {
        MarriageAgency marriageAgency = new MarriageAgencyImpl();
        Person person = new Person("张三",18,Sex.MALE,20,marriageAgency);
        Person person1 = new Person("李四",19,Sex.MALE,28,marriageAgency);
        Person person2 = new Person("王五",20,Sex.FEMALE,21,marriageAgency);
        Person person3 = new Person("赵六",28,Sex.FEMALE,21,marriageAgency);
        Person person4 = new Person("胡七",18,Sex.FEMALE,29,marriageAgency);
        person1.findPartner();

    }
}

// 中介者类
interface MarriageAgency{
    void regisiter(Person person);// 注册会员
    void pair(Person person);     // 为Person配对
}
// 参与者
class Person{
    String name;
    int age;
    Sex sex;
    int requestAge;
    MarriageAgency agency;

    public Person(String name, int age, Sex sex, int requestAge, MarriageAgency agency) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.requestAge = requestAge;
        this.agency = agency;
        agency.regisiter(this);
    }
    public void findPartner(){
        agency.pair(this);
    }
}
enum Sex{
    MALE,FEMALE;
}
// 中介者实现类
class MarriageAgencyImpl implements MarriageAgency{

    List<Person> people =new ArrayList();

    @Override
    public void regisiter(Person person) {
        people.add(person);
    }

    @Override
    public void pair(Person person) {
        for(Person p : people){
            if(p.age== person.requestAge&& p.sex!=person.sex){
                System.out.println("将"+person.name+"与"+p.name+"送入洞房");
            }
        }
    }
}